void main() {
  ShoppingCart.withCode(name: '张三', code: '123456')
    ..bookings = [Item('苹果', 10.0), Item('鸭梨', 20.0)]
    ..printInfo();
  ShoppingCart(name: '李四')
    ..bookings = [Item('香蕉', 15.0), Item('西瓜', 40.0)]
    ..printInfo();
}

/// Meta类
class Meta {
  String name;
  double price;
  Meta(this.name, this.price);
}

/// 定义商品Item类
class Item extends Meta {
  Item(name, price) : super(name, price);

  /// 重载+运算符, 合并商品为套餐商品
  Item operator +(Item item) => Item(name + item.name, price + item.price);
}

/// 打印类接口
abstract class PrintHelper {
  printInfo() => print(getInfo());
  getInfo();
}

/// 定义购物车类
/// with: 表示以非继承的方式复用了另一个类的成员变量及方法
class ShoppingCart extends Meta with PrintHelper {
  DateTime date;
  String code;
  List<Item> bookings = [];

  // reduce:遍历数组中前一个元素和后一个元素
  double get price => bookings.reduce((current, next) => current + next).price;

  // 默认初始化函数，转发至withCode函数
  ShoppingCart({name}) : this.withCode(name: name);
  ShoppingCart.withCode({name, this.code = "没有"})
      : date = DateTime.now(),
        super(name, 0);

  @override
  getInfo() => '''
购物车信息:
-----------------------------
  用户名: $name
  优惠码: $code
  总价: $price
  日期: $date
-----------------------------
  ''';
}
